// pages/points/pointsapplication/pointsapplication.js
const wxapi = require('../../../utils/wxapi.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      depArray:[],
      depIndex:0,
      depValue:0,
      whoArray:[
        "管理员",
        "超级管理员",
        "总裁",
      ],
      whoValue:0,
      whoIndex:0,
      typeArray:[],
      typeIndex:0,
      typeValue:0,
      reason:'',
      num:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let sec = wx.getStorageSync("secKey")
    this.getDepData(sec);
    // this.getWhoData(sec);
    this.getTypeData(sec);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindDepPickerChange: function (e) {
    this.setData({
      depIndex: e.detail.value
    })
  },
  bindWhoPickerChange: function (e) {
    this.setData({
      whoIndex: e.detail.value
    })
  },
  bindTypePickerChange: function (e) {
    this.setData({
      typeIndex: e.detail.value
    })
  },
  getDepData(sec){
    let that = this;
    wxapi.getDepData(sec).then(res=>{
        that.setData({
          depArray:res.data
        })
    })
  },
  getTypeData(sec){
    let that = this;
    wxapi.getTypeData(sec).then(res=>{
      that.setData({
        typeArray: res.data
      })
    })
  },
  getWhoData(sec){
    let that = this;
    wxapi.getTypeData(sec).then(res => {
      that.setData({
        whoArray: res.data
      })
    })
  },
  bindSubmit(e){
    console.log(e)
    let data = e.detail.value;
    if(data.num==""||data.num==0){
      wx.showModal({
        title: '注意!',
        content: '请正确填写积分数量',
        showCancel:false
      })
      return;
    }
    if (data.reason==""){
      wx.showModal({
        title: '注意!',
        content: '请正确填写申请积分理由',
        showCancel: false
      })
      return;
    }
    data.sercet = wx.getStorageSync('secKey');
    wxapi.submitPointsForm(data).then(res=>{
      if(res.suc){
        wx.switchTab({
          url: '/pages/points/points',
        })
      }
    }).catch(err=>{
      wx.showModal({
        title: '错误',
        content: err,
      })
    })
  }


})